import { Module } from 'vuex';
// 此处加上 `.ts` 后缀报错，具体原因不详
import { RootStateTypes, SystemsState } from '../interface';
import { querySystemInfo } from "/@/api/commomApi";
import { userTempLogin } from "/@/api/AuthApi";
import { uuid } from "/@/util";
import storageUtil from "/@/util/storageUtil";



const systemModule: Module<SystemsState, RootStateTypes> = {
    namespaced: true,
    state: {
        systemCode: null,
        wxMpAppId: null,//微信授权appid
        // baseConfig: {
        //     selectStore: null,
        //     registerInputPlateNumber: null
        // }
    } as any,
    mutations: {
        // 设置用户信息
        setSystemDTO(state, data: any) {
            console.log('SET_SYSTEM_DTO-----', data)
            state.systemCode = data.systemCode;
            state.wxMpAppId = data.weChatObject.wechatAppId;
            storageUtil.setSystemCode(data.systemCode)

            // state.baseConfig = data.baseConfig || { selectStore: null }
        },
        // 设置用户信息
        removeSystemDTO(state: any, data: object) {
            state.systemCode = null
            state.wxMpAppId = null
            state.baseConfig = { selectStore: null }
        },
    },
    actions: {
        // 获取系统配置
        async getSystemInfo({ commit, state }) {
            if (state.systemCode) {
                return Promise.resolve({ ...state })
            }
            return new Promise<any>((resolve, reject) => {
                console.log('GET_SYSTEM_INFO_SYNC====44444')
                querySystemInfo({
                    domainUrl: window.location.hostname,
                    accUrl: window.location.href,
                    sharesystemSignId: null,
                    shareId: null,
                    systemCode: state.systemCode ? state.systemCode : null,

                }).then((res) => {
                    commit('setSystemDTO', res)
                    resolve(res)
                }).catch(() => {
                    reject()
                })
            })
        },
        // 获取系统配置
        async getSystemInfoSync({ commit, state }) {
            return new Promise<any>((resolve, reject) => {
                querySystemInfo({
                    domainUrl: window.location.hostname,
                    accUrl: window.location.href,
                    sharesystemSignId: null,
                    shareId: null,
                    systemCode: state.systemCode ? state.systemCode : null,

                }).then((res) => {
                    commit('setSystemDTO', res)
                    resolve(res)
                }).catch(() => {
                    reject()
                })
            })
        },
       

        // 获取临时登录
        async getUserTempLogin() {
            return new Promise<any>((resolve, reject) => {
                userTempLogin({
                    userAttr: '4', //默认游客
                    loginType: '0',
                    userId: uuid(),
                }).then((res) => {
                    // commit('setSystemDTO', res)
                    resolve(res)
                }).catch(() => {
                    reject()
                })
            })
        },
    },
};

export default systemModule;
